package cn.tedu.mall.book.controller;

import cn.tedu.mall.book.pojo.dto.ProductAddNewDTO;
import cn.tedu.mall.book.pojo.dto.ProductUpdateDTO;
import cn.tedu.mall.book.pojo.vo.ProductSimpleVO;
import cn.tedu.mall.book.pojo.vo.ProductDetailVO;
import cn.tedu.mall.book.service.IProductService;
import cn.tedu.mall.book.web.JsonPage;
import cn.tedu.mall.book.web.JsonResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zxjun
 * @date 2022/6/10 20:41
 */
@Api(tags = "商品管理控制层")
@RestController
@RequestMapping(value = "/products", produces = "application/json;charset=utf-8")
public class ProductController {
    @Autowired
    IProductService service;

    @ApiOperation("新增商品功能")
    @PostMapping("/add")
    public JsonResult<Void> addNewProduct(@RequestBody ProductAddNewDTO productAddNewDTO){
        System.out.println("ProductAddNewDTO >> "+productAddNewDTO);
        service.addNewProduct(productAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("后台修改商品")
    @PostMapping("/update")
    public JsonResult updateById(@RequestBody ProductUpdateDTO productUpdateDTO){
        service.updateById(productUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("后台删除商品")
    @GetMapping("/delete/{id}")
    public JsonResult deleteById(@PathVariable Long id){
        service.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("后台分页查询商品")
    @GetMapping("/list")
    public JsonResult<JsonPage<ProductSimpleVO>> listPage(Integer page,Integer pageSize){
        return JsonResult.ok(service.getList(page,pageSize));
    }

    @GetMapping("/get/{id}")
    public JsonResult<ProductDetailVO> getById(@PathVariable Long id){
        return JsonResult.ok(service.getById(id));
    }

    @GetMapping("/getBookForNewStart")
    public JsonResult<List<ProductSimpleVO>> getBookForNewStart() {
        return JsonResult.ok(service.getBookForNewStart());
    }

    @GetMapping("/getBookForRecommend")
    public JsonResult<List<ProductSimpleVO>> getBookForRecommend() {
        return JsonResult.ok(service.getBookForRecommend());
    }

    @GetMapping("/getNewBookTop")
    public JsonResult<List<ProductSimpleVO>> getNewBookTop() {
        return JsonResult.ok(service.getBookForNewBookTop());
    }

    @GetMapping("/getBestSellerTop")
    public JsonResult<List<ProductSimpleVO>> getBestSellerTop() {
        return JsonResult.ok(service.getBookForBestSellerTop());
    }

    /**
     * 接收一级分类id 返回所有的商品
     */
    @GetMapping("/getProductsByCategoryId/{id}")
    public JsonResult<List<ProductSimpleVO>> getProductsByCategoryId(@PathVariable Integer id){
        return JsonResult.ok(service.getProductsByCategoryId(id));
    }

    @GetMapping("/getProductsByCategoryIdOrderly/{id}/{index}")
    public JsonResult<List<ProductSimpleVO>> getProductsByCategoryIdOrderly(@PathVariable("id") Integer id,@PathVariable("index") String index){
        return JsonResult.ok(service.getProductsByCategoryChildrenIdOrderly(id,index));
    }

    /**
     * 根据关键词查询所有商品并返回所有的商品
     */
    @GetMapping("/getProductsBySearch/{wd}")
    public JsonResult<List<ProductSimpleVO>> getProductsBySearch(@PathVariable String wd){
        return JsonResult.ok(service.getProductsBySearch(wd));
    }

    /**
     * 根据关键词查询所有商品并按一定顺序返回
     */
    @GetMapping("/getProductsBySearch/{wd}/{index}")
    public JsonResult<List<ProductSimpleVO>> getProductsBySearchOrderly(@PathVariable String wd,@PathVariable String index){
        return JsonResult.ok(service.getProductsBySearchOrderly(wd, index));
    }

}
